python-3-object-oriented-programming by Unknown
Author:Unknown
Language: rus
Format: epub
Tags: www.it-ebooks.info
Published: 2015-08-14T10:57:54+00:00
Python Object-oriented Shortcuts
Keyword arguments are also very useful when we need to accept arbitrary arguments to pass to a second function, but we don't know what those arguments will be. We saw this in action in Chapter 3, When Objects Are Alike, when we were building support for multiple inheritance. We can, of course, combine the variable argument and variable keyword argument syntax in one function call, and we can use normal positional and default arguments as well. The following example is somewhat contrived, but demonstrates the four types in action:
import shutil
import os.path
def augmented_move(target_folder, *filenames,
verbose=False, **specific):
'''Move all filenames into the target_folder, allowing
specific treatment of certain files.'''
def print_verbose(message, filename):
'''print the message only if verbose is enabled'''
if verbose:
print(message.format(filename))
for filename in filenames:
target_path = os.path.join(target_folder, filename)
if filename in specific:
if specific[filename] == 'ignore':
print_verbose("Ignoring {0}", filename)
elif specific[filename] == 'copy':
print_verbose("Copying {0}", filename)
shutil.copyfile(filename, target_path)
else:
print_verbose("Moving {0}", filename)
shutil.move(filename, target_path)
This example will process an arbitrary list of files. The first argument is a target folder, and the default behavior is to move all remaining non-keyword argument files into that folder. Then there is a keyword-only argument, verbose, which tells us whether to print information on each file processed. Finally, we can supply a dictionary containing actions to perform on specific filenames; the default behavior is to move the file, but if a valid string action has been specified in the keyword arguments, it can be ignored or copied instead. Notice the ordering of the parameters in the function; first the positional argument is specified, then the *filenames list, then any specific keyword-only arguments, and finally, a **specific dictionary to hold remaining keyword arguments.
[ 210 ]
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Brazilian Economy since the Great Financial Crisis of 20072008 by Philip Arestis Carolina Troncoso Baltar & Daniela Magalhães Prates(100537)
International Integration of the Brazilian Economy by Elias C. Grivoyannis(70573)
The Art of Coaching by Elena Aguilar(52090)
Flexible Working by Dale Gemma;(23197)
How to Stop Living Paycheck to Paycheck by Avery Breyer(19543)
The Acquirer's Multiple: How the Billionaire Contrarians of Deep Value Beat the Market by Tobias Carlisle(12073)
Thinking, Fast and Slow by Kahneman Daniel(11694)
The Radium Girls by Kate Moore(11573)
The Art of Thinking Clearly by Rolf Dobelli(9817)
Hit Refresh by Satya Nadella(8824)
The Compound Effect by Darren Hardy(8443)
Atomic Habits: Tiny Changes, Remarkable Results by James Clear(7989)
Tools of Titans by Timothy Ferriss(7744)
Turbulence by E. J. Noyes(7665)
Change Your Questions, Change Your Life by Marilee Adams(7324)
Nudge - Improving Decisions about Health, Wealth, and Happiness by Thaler Sunstein(7200)
A Court of Wings and Ruin by Sarah J. Maas(7160)
How to Be a Bawse: A Guide to Conquering Life by Lilly Singh(7137)
Win Bigly by Scott Adams(6793)
